<?php
/**
 * 短信控制类API
 * @author tangbl
 *
 */
class SmsAction extends CommonAction {
	
	public function _initialize(){
		
		parent::_initialize();
	    //parent::checkTimestamp();
	}
		
	/**
	 * 群发短信
	 */
	public function send(){
		
		$model   = D("Tip");
		$table   = $this->_req("table");
		$tempid  = $this->_req("tempid");
		$text    = $this->_req("content");
		$mobile  = $this->_req("mobile");
		$addid   = $this->_req("addid");
		
		if($tempid){
			$content = $model->getSmsTemplate($tempid);
		}else{
			$content = $text;
		}
		
		$solevar = create_id();
		$array   = array("addid"=>$addid,"content"=>$content,"table"=>$table,
						 "mobile"=>$mobile,"solevar"=>$solevar);
		$newid   = $model->addSmsSend($array);
		
		$send    = 0;
		$succ    = 0;
		$fail    = 0;
		$temp    = 0;
		$phone   = 0;
		$illegal = "";
		$failure = "";
		
		$rows['solevar'] = $solevar;
		$rows['content'] = $content;
		$rows['addid']   = $addid;
		
		$url = createMobile();
		if($table){
			$moudle  = M("$table");
			$list    = $moudle->field("id,mobile")->select();
			foreach($list as $v){
				if(!ismobile($v['mobile'])){
					$illegal .= $v['mobile']."|";
					continue;
				}
				$send++;$temp++;
				//$info = sendSms($url,$v['mobile'],$content,false);
				$info = array(1);
				if($info[0]==1){
					$succ++;
					$rows['status'] = 1;
				}else{
					$fail++;
					$rows['status'] = 2;
					$failure .= $v['mobile']."|";
				}
				$rows['mobile'] = $v['mobile'];
				$model->smsSendLog($rows);
			}
		}
		
		if($mobile){
			$lists   = explode(",", $mobile);
			
			foreach($lists as $v){
				if(!ismobile($v)){
					$illegal .= $v."|";
					continue;
				}
				$send++;
				//$info = sendSms($url,$v['mobile'],$content,false);
				$info = array(1);
				if($info[0]==1){
					$succ++;
					$rows['status'] = 1;
				}else{
					$fail++;
					$rows['status'] = 2;
					$failure .= $v."|";
				}
				$rows['mobile'] = $v;
				$model->smsSendLog($rows);
			}
		}
		
		$row = array("id"=>$newid,"illegal"=>$illegal,"failure"=>$failure,"succ"=>$succ,"send"=>$send,"fail"=>$fail);
		$model->saveSmsSend($row);
		$this->success("",L("sms_many_send_successTip"));
	}
	
	/**
	 * 单发短信
	 */
	public function single(){
		
		$content = $this->_req("content");
		$mobile  = $this->_req("mobile");
		$tempid  = $this->_req("sign");
	
		if(!ismobile($mobile)){
			$this->error(L("sms_mobile_regex"));
		}	
		
		if($tempid){
			$content = $this->dealSmsTemp($tempid);
		}
		
		$info = $this->sms($mobile,$content);
		
		if($info[0] == 1){
			$this->success("",L("sms_note_send_successTip"));
		}else{
			$this->error($info[1]);
		}
	}
	
	/**
	 * 通用处理短信模板
	 */
	public function dealSmsTemp($tempid){
		
		$model = D("Tip");
		
		$content = $model->getSmsTemplate($tempid);
		$this->assign("detail",$this->input());
		return $this->fetch("",$content);
	}
	
	/**
	 * 发送短信公共操作
	 */
	public function sms($mobile,$content){
		
		$url  = createMobile();
		$info = sendSms($url,$mobile,$content);
		return $info;
	}
	
}